import { MOVE_SPEED } from "./Constant";
import { Utils } from "./Utils";

const { regClass, property } = Laya;

@regClass()
export class Player extends Laya.Script {
    //declare owner : Laya.Sprite3D;
    declare owner: Laya.Sprite;

    @property(Laya.GTextField)
    public labName: Laya.GTextField;

    @property(Laya.GImage)
    public imgHead: Laya.GImage;

    @property(Laya.Animation)
    public aniPlayer: Laya.Animation;

    private is_moveing = false;

    private gridX_start: number = 0;
    private gridY_start: number = 0;

    private tiled_width: number = 0;
    private tiled_height: number = 0;

    private runPaths: any[] = [];

    public userInfo: any

    //组件被激活后执行，此时所有节点和组件均已创建完毕，此方法只执行一次
    //onAwake(): void {}

    //组件被启用后执行，例如节点被添加到舞台后
    onEnable(): void {

    }

    //组件被禁用时执行，例如从节点从舞台移除后
    onDisable(): void {

    }

    //第一次执行update之前执行，只会执行一次
    onStart(): void {

    }

    setPlayInfo(info: any, isSelf?: boolean) {
        this.userInfo = info;
        this.labName.text = isSelf ? "自己" : info.userName;
        this.labName.color = isSelf ? Laya.Color.RED.toString() : Laya.Color.WHITE.toString();
        this.imgHead.icon = Utils.getServerBaseUrl() + info.userAvatar;
    }

    setStarGrid(x: number, y: number, tw: number, th: number) {
        this.gridX_start = x;
        this.gridY_start = y;
        this.tiled_width = tw;
        this.tiled_height = th;
        this.owner.pos(x * tw + tw / 2, y * th + th / 2);
    }

    get StartGridX(): number {
        return this.gridX_start;
    }

    get StartGridY(): number {
        return this.gridY_start;
    }

    playIdle(keepDir?: boolean) {
        this.aniPlayer.loadAtlas("resources/frames/player_idle.atlas");
        if (!keepDir)
            this.aniPlayer.scaleX = Utils.randomRange(0, 100) < 50 ? 1 : -1;
        this.is_moveing = false;
        this.runPaths.length = 0;
        this.aniPlayer.index = 0;
        this.aniPlayer.play(0, true);
    }

    playRun(path: any[]) {
        this.aniPlayer.loadAtlas("resources/frames/player_walk_left.atlas");
        this.aniPlayer.scaleX = 1;
        this.is_moveing = true;
        this.aniPlayer.index = 0;
        this.aniPlayer.play(0, true);
        this.runPaths = path;
    }

    public get isMoving() {
        return this.is_moveing;
    }

    //手动调用节点销毁时执行
    //onDestroy(): void {}

    //每帧更新时执行，尽量不要在这里写大循环逻辑或者使用getComponent方法
    onUpdate(): void {
        if (!this.is_moveing)
            return;
        if (this.runPaths.length > 0) {
            let path = this.runPaths[0];
            let x = path.x * this.tiled_width + this.tiled_width / 2;
            let y = path.y * this.tiled_height + this.tiled_height / 2;
            if (x > this.owner.x) {
                this.aniPlayer.scaleX = -1;
            } else if (x < this.owner.x) {
                this.aniPlayer.scaleX = 1;
            }

            if (Math.abs(x - this.owner.x) < MOVE_SPEED && Math.abs(y - this.owner.y) < MOVE_SPEED) {
                this.owner.pos(x, y);
                let p = this.runPaths.shift();
                if (this.runPaths.length == 0) {
                    this.playIdle(true);
                    this.gridX_start = p.x;
                    this.gridY_start = p.y;
                }
            } else {
                if (Math.abs(x - this.owner.x) >= MOVE_SPEED) {
                    if (x > this.owner.x) {
                        this.owner.x += MOVE_SPEED;
                    } else {
                        this.owner.x -= MOVE_SPEED;
                    }
                } else if (Math.abs(y - this.owner.y) >= MOVE_SPEED) {
                    if (y > this.owner.y) {
                        this.owner.y += MOVE_SPEED;
                    } else {
                        this.owner.y -= MOVE_SPEED;
                    }
                }
            }
        }
    }

    //每帧更新时执行，在update之后执行，尽量不要在这里写大循环逻辑或者使用getComponent方法
    //onLateUpdate(): void {}

    //鼠标点击后执行。与交互相关的还有onMouseDown等十多个函数，具体请参阅文档。
    //onMouseClick(): void {}

}